# -*- coding: utf-8 -*-
__author__ = 'sunnychou'
__date__ = '2019/9/10 14:39'
import re
#月份应收账款分析表
from k3_data_report.finance.config import finace_cal_fields
from k3_data_report.finance.utils import cal_finance_jine_select
from common.utils import *
from k3_data_report.finance.utils import cal_ysk_by_timerange
#from config import *
from common.mssql_helper import g_msql_inst
from common.logger_helper import g_wlogger
from k3_data_report.finance.kh_business_dp import create_df_k3jyqks

'''
应收账款分析表
此表结合经营情况报表内容，加入如下内容：
    （1）月份到期应收
    （2）月份超期应收
    （3） 《 30天
    （4） 30~60天
计算方式：
    明细里面的单据日期，采购订单里面订单日期，采购出库里面的出库日期 进行时间判断标准
    当前计算8月份的，月结90天的，那就是单据日期是5月份的，计算出金额
'''

class  CalTimeRangeJsJine(object):
    def __init__(self, ymonth):
        self.__ymonth = ymonth
        self.__less_30_days_groupby_dict = {}
        self.__between_30_60_groupby_dict = {}
        self.__between_60_90_groupby_dict = {}
        self.__between_90_120_groupby_dict = {}
        self.__between_120_150_groupby_dict = {}
        self.__between_150_180_groupby_dict = {}
        self.__process()


    def __process(self):
        # 1. 最近30天的
        (tmonth_fday, tmonth_lday) = get_today_month(self.__ymonth, 0)
        tmonth_fday_month = get_month(tmonth_fday)
        self.__less_30_days_groupby_dict = cal_ysk_by_timerange(tmonth_fday_month, tmonth_fday_month)
        # 2. 30~60天
        (th_2_fday, th_2_lday) = get_today_month(self.__ymonth, -1)
        th_2_fday_month = get_month(th_2_fday)
        self.__between_30_60_groupby_dict = cal_ysk_by_timerange(th_2_fday_month, th_2_fday_month)
        # 3. 60~90天
        (th_3_fday, th_3_lday) = get_today_month(self.__ymonth, -2)
        th_3_fday_month = get_month(th_3_fday)
        self.__between_60_90_groupby_dict = cal_ysk_by_timerange(th_3_fday_month, th_3_fday_month)
        # 4. 90~120天
        (th_4_fday, th_4_lday) = get_today_month(self.__ymonth, -3)
        th_4_fday_month = get_month(th_4_fday)
        self.__between_90_120_groupby_dict = cal_ysk_by_timerange(th_4_fday_month, th_4_fday_month)
        # 5. 120~150天
        (th_5_fday, th_5_lday) = get_today_month(self.__ymonth, -4)
        th_5_fday_month = get_month(th_5_fday)
        self.__between_120_150_groupby_dict = cal_ysk_by_timerange(th_5_fday_month, th_5_fday_month)
        # 6. 150~180天
        (th_6_fday, th_6_lday) = get_today_month(self.__ymonth, -5)
        th_6_fday_month = get_month(th_6_fday)
        self.__between_150_180_groupby_dict = cal_ysk_by_timerange(th_6_fday_month, th_6_fday_month)


    def to_dict(self):
        res = dict(
            less_30_days_groupby_dict = self.__less_30_days_groupby_dict,
            between_30_60_groupby_dict = self.__between_30_60_groupby_dict,
            between_60_90_groupby_dict = self.__between_60_90_groupby_dict,
            between_90_120_groupby_dict = self.__between_90_120_groupby_dict,
            between_120_150_groupby_dict = self.__between_120_150_groupby_dict,
            between_150_180_groupby_dict = self.__between_150_180_groupby_dict,
        )
        return res

def fetch_time_range_dict_jine(days_groupby_dict, kehuname, jiesuan_type):
    days_jine = 0
    if re.search(r"款到", jiesuan_type) != None or re.search(r"现金", jiesuan_type) != None:  # 货到,销售出库
        days_jine_dict = days_groupby_dict[3]
        days_jine = days_jine_dict.get(kehuname, 0)
        days_jine = days_groupby_dict[2].get(kehuname, 0) if days_jine == 0 else days_jine
        days_jine = days_groupby_dict[1].get(kehuname, 0) if days_jine == 0 else days_jine
    elif re.search(r"月结", jiesuan_type) != None:  # 货到,销售出库
        days_jine_dict = days_groupby_dict[2]
        days_jine = days_jine_dict.get(kehuname, 0)
        days_jine = days_groupby_dict[1].get(kehuname, 0) if days_jine == 0 else days_jine
        days_jine = days_groupby_dict[3].get(kehuname, 0) if days_jine == 0 else days_jine
    else:
        days_jine_dict = days_groupby_dict[1]
        days_jine = days_jine_dict.get(kehuname, 0)
        days_jine = days_groupby_dict[2].get(kehuname, 0) if days_jine == 0 else days_jine
        days_jine = days_groupby_dict[3].get(kehuname, 0) if days_jine == 0 else days_jine
    return days_jine


def create_df_kh_yskfx_rmb(last_month, pay_type="人民币"):

    k3jyqks_df = create_df_k3jyqks(last_month, pay_type)

    ctrje__groupby_dict = CalTimeRangeJsJine(last_month).to_dict()
    less_30_days_groupby_dict = ctrje__groupby_dict['less_30_days_groupby_dict']
    between_30_60_groupby_dict = ctrje__groupby_dict['between_30_60_groupby_dict']
    between_60_90_groupby_dict = ctrje__groupby_dict['between_60_90_groupby_dict']
    between_90_120_groupby_dict = ctrje__groupby_dict['between_90_120_groupby_dict']
    between_120_150_groupby_dict = ctrje__groupby_dict['between_120_150_groupby_dict']
    between_150_180_groupby_dict = ctrje__groupby_dict['between_120_150_groupby_dict']

    less_30_days_jine_list = []
    between_30_60_jine_list = []
    between_60_90_jine_list = []
    between_90_120_jine_list = []
    between_120_150_jine_list = []
    between_150_180_jine_list = []
    for (kehuname,  jiesuan_type) in k3jyqks_df[['客户名称', '款期']].values:
        #if re.search(r"款到", jiesuan_type) != None or re.search(r"现金", jiesuan_type) != None:  # 货到,销售出库
        #获取 最近30天的 的金额
        less_30_days_jine = fetch_time_range_dict_jine(less_30_days_groupby_dict, kehuname, jiesuan_type)
        less_30_days_jine_list.append(less_30_days_jine)
        #获取30~60天的金额
        between_30_60_jine = fetch_time_range_dict_jine(between_30_60_groupby_dict, kehuname, jiesuan_type)
        between_30_60_jine_list.append(between_30_60_jine)
        #获取60~90天的金额
        between_60_90_jine = fetch_time_range_dict_jine(between_60_90_groupby_dict, kehuname, jiesuan_type)
        between_60_90_jine_list.append(between_60_90_jine)
        #获取90~120天的金额
        between_90_120_jine = fetch_time_range_dict_jine(between_90_120_groupby_dict, kehuname, jiesuan_type)
        between_90_120_jine_list.append(between_90_120_jine)
        #获取120~150天的金额
        between_120_150_jine = fetch_time_range_dict_jine(between_120_150_groupby_dict, kehuname, jiesuan_type)
        between_120_150_jine_list.append(between_120_150_jine)
        #获取150~180天的金额
        between_150_180_jine = fetch_time_range_dict_jine(between_150_180_groupby_dict, kehuname, jiesuan_type)
        between_150_180_jine_list.append(between_150_180_jine)
        print(f"kehuname:{kehuname}, less_30_days_jine:{less_30_days_jine}, between_30_60_jine:{between_30_60_jine},"
              f"between_60_90_jine:{between_60_90_jine}, between_90_120_jine:{between_90_120_jine}")
    k3jyqks_df["<30天应收"] = less_30_days_jine_list
    k3jyqks_df["30~60天应收"] = between_30_60_jine_list
    k3jyqks_df["60~90天应收"] = between_60_90_jine_list
    k3jyqks_df["90~120天应收"] = between_90_120_jine_list
    k3jyqks_df["120~150天应收"] = between_120_150_jine_list
    k3jyqks_df["150~180天应收"] = between_150_180_jine_list

    return k3jyqks_df


def create_df_kh_yskfx_mj(last_month, pay_type="美金"):
    return create_df_kh_yskfx_rmb(last_month, pay_type)



if __name__ == "__main__":
    month = "2019-07"
    k3jyqks_df = create_df_kh_yskfx_rmb(month)

    #print(k3jyqks_df)





